package tuwien.big.formel0.picasa;

import com.google.gdata.client.Query;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.TagEntry;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lukas
 */
public class RaceDriverService implements IRaceDriverService {
    static int test = 0;

    @Override
    public List<RaceDriver> getRaceDrivers() throws IOException, ServiceException {
        List<RaceDriver> result = new ArrayList<RaceDriver>();
        
        URL albumUrl;
        try {
            albumUrl = new URL("https://picasaweb.google.com/data/feed/api/user/107302466601293793664");
        } catch (MalformedURLException ex) {return null;}
        PicasawebService myService = new PicasawebService("lab4");
        
        Query myQuery = new Query(albumUrl);
        myQuery.setStringCustomParameter("kind", "photo");
        myQuery.setStringCustomParameter("tag", "Driver");
        myQuery.setStringCustomParameter("imgmax", "144");
        
        AlbumFeed feed = myService.query(myQuery, AlbumFeed.class);
        
        for (PhotoEntry photo : feed.getPhotoEntries()) {
            RaceDriver av = parseAvatar(photo, myService);
            if (av != null) {
                result.add(av);
            }
        }
        return result;
    }
    
    private RaceDriver parseAvatar(PhotoEntry photo, PicasawebService myService) throws IOException, ServiceException {
        RaceDriver avatar = new RaceDriver();
        avatar.setName(photo.getDescription().getPlainText());
        avatar.setUrl(getPhotoUrl(photo));
        avatar.setWikiUrl(getWikiUrl(photo, myService));
        if (avatar.getWikiUrl() == null || avatar.getUrl() == null || avatar.getName() == null) {
            return null;
        }
        return avatar;
    }
    
    private String getPhotoUrl(PhotoEntry photo) {
        return photo.getMediaContents().get(0).getUrl();
    }
    
    private String getWikiUrl(PhotoEntry photo, PicasawebService myService) {
        URL feedUrl;
        try {
            feedUrl = new URL(photo.getId().replace("entry", "feed/api"));
        } catch (MalformedURLException ex) {return null;}
        Query query = new Query(feedUrl);
        query.setStringCustomParameter("kind", "tag");
        AlbumFeed searchResultsFeed;
        try {
            searchResultsFeed = myService.query(query, AlbumFeed.class);
        } catch (IOException ex) {
            return "IOException";
        } catch (ServiceException ex) {
            return "ServiceException";
        }
        
        for (TagEntry tag : searchResultsFeed.getTagEntries()) {
            String s = tag.getTitle().getPlainText();
            if (s.startsWith("wiki:")) {
                return s.substring(5);
            }
        }
        return "fail";
    }
}
